package com.bank.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bank.entity.CustomerInfo;
import com.bank.exception.CommonException;
import com.bank.service.CustomerService;


/*客户信息主控制类*/
@Controller
public class CustomerController extends BaseController {
	
	Logger logger = Logger.getLogger(CustomerController.class);
	
	@Autowired
	CustomerService customerService;
	
	@RequestMapping(value="doAddCustomer.do", method = RequestMethod.POST)
	public  @ResponseBody Map<String,Object> doAddClient(@RequestBody Map<String,Object> params) throws CommonException{
		customerService.addCustomer(params);
		return this.setAndGetSuccess(); 
	}
	
	
	@RequestMapping(value="doQueryCustomer.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> doQueryCustomer(@RequestBody Map<String,Object> params) throws CommonException{
		String customer_number = (String) params.get("customer_number");
		//CustomerInfo customer=customerService.queryCustomer(customer_number);
		CustomerInfo customer = customerService.selectByIdno(params);
		if(customer == null){
			return this.setAndGetFailed();
		}
		this.setMapValue("customer_id", customer.getCustomer_number());
		this.setMapValue("customer_number", customer.getCustomer_number());
		this.setMapValue("customer_name", customer.getCustomer_name());
		this.setMapValue("customer_id_type", customer.getCustomer_id_type());
		this.setMapValue("customer_idno", customer.getCustomer_idno());
		this.setMapValue("customer_phone", customer.getCustomer_phone());
		this.setMapValue("customer_email", customer.getCustomer_email());
		this.setMapValue("customer_address", customer.getCustomer_address());
		return this.setAndGetSuccess();
	}
	
	@RequestMapping(value="doUpdateCustomer.do", method = RequestMethod.POST)
	public @ResponseBody Map<String,Object> doUpdateCustomer(@RequestBody Map<String,Object> params) throws CommonException{
		logger.debug("开始更新数据");
		customerService.updateCustomer(params);
		return this.setAndGetSuccess();
	}
	
	@RequestMapping(value="doBatchImport.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> doBatchImport(HttpServletRequest request, @RequestParam MultipartFile file_name) throws Exception{
		if(file_name.isEmpty()){
			return this.setAndGetFailed();
		}
		int result = customerService.batchImport(file_name);
		if(result == -1){
			return this.setAndGetFailed();
		}
		this.setMapValue("number", String.valueOf(result));
		return  this.setAndGetSuccess();
	}
	
	
	

}
